#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<stdlib.h>

struct ListNode {
    int val;
    struct ListNode* next;
   
};

//struct ListNode* reverseList(struct ListNode* head) {
//   
//    if (head == NULL)
//    {
//        return NULL;
//    }
//            
//    struct ListNode* cur = head->next;
//    struct ListNode* ahead =head;
//    struct ListNode* tmp = NULL;
//
//    if (head == NULL)
//    {
//        return NULL;
//    }
//    ahead->next = NULL;
//    while (cur)
//    {
//        if (cur->next != NULL)
//        {
//            tmp = cur->next;
//            cur->next = NULL;
//            cur->next = ahead;
//            ahead = cur;
//            cur = tmp;
//        }
//        else
//        {
//            cur->next = ahead;
//            ahead = cur;
//            cur = NULL;
//        }
//
//    }
//
//    return ahead;
//}


struct ListNode* reverseList(struct ListNode* head)
{
    struct ListNode* cur = head;
    struct ListNode* ahead = NULL;
    struct ListNode* tmp = NULL;

    while (cur)
    {

        tmp = cur->next;
        cur->next = ahead;
        ahead = cur;
        cur = tmp;

    }

    return ahead;

}


int main()
{
    struct ListNode* n1 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* n2 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* n3 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* n4 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* n5 = (struct ListNode*)malloc(sizeof(struct ListNode));
    n1->val = 1;
    n2->val = 2;
    n3->val = 3;
    n4->val = 4; 
    n5->val = 5;
    n1->next = n2;
    n2->next = n3;
    n3->next = n4;
    n4->next = n5;
    n5->next = NULL;
    struct ListNode*head= reverseList(n1);


    return 0;
}